package com.ydc.ln.commons.utils;

import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

/**
 * 字符串处理工具类
 * @author ydc
 *
 */
public class StringUtil {
	
	@Test
	/**
	 * 处理字符串，决断字符串最后一个字符是不是","，是的话删除
	 */
	public void proString() {
		String str = "dddfdfd,dfdfdf,";
		if(str.endsWith(",")) {
			str = str.substring(0, str.length()-1);
		}
		System.out.println(str);
	}
	
	@Test
	public void parseString() {
		System.out.println(Float.valueOf("0.0"));
	}
	@Test
	public void replaceString() {
		String str = "根元素@#&子元素@#&属性@#&";
		str = str.replaceAll("@#&", ",");
		System.out.println(str);
	}
	
	@Test
	public void formatString2() {
		String str = "根元素@#&子元素@#&属性@#&";
		String[] strArray = str.split("@#&");
		for (String s : strArray) {
			System.out.println(s);
		}
	}
	
	
	@Test
	public void formatString() {
		String str = "0,uni_qn,grade_2009,QN200901,QN200902,grade_2010,QN201001,QN201002,uni_wf,grade_2009,grade_2010";
		// 去掉  "0,"
		str = str.substring(str.indexOf(",") + 1);
		String[] uniArray = str.split("uni_");
		// 遍历学校
		for (int i=1; i<uniArray.length; i++) {
			String uni = uniArray[i];
			// 去掉qn,
			String uniName = uni.substring(0, uni.indexOf(","));
			System.out.println("学校名称：" + uniName);
			uni = uni.substring(uni.indexOf(",") + 1);
			String[] gradeArray = uni.split("grade_");
			// 遍历年级
			for (int j=1; j<gradeArray.length; j++) {
				String grade = gradeArray[j];
				String gradeName = grade.substring(0, grade.indexOf(","));
				System.out.println("\t学年：" + gradeName);
				grade = grade.substring(grade.indexOf(",") + 1);
				String[] classArray = grade.split(",");
				// 遍历班级
				for (int k=0; k<classArray.length; k++) {
					System.out.println("\t\t" + classArray[k]);
				}
			}
		}
	}
	/**
	 * Java 复制字符串到系统剪贴板
	 */
	public void copy2Clipboard() {
		StringSelection stsel = new StringSelection("String");
		Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stsel, stsel);
	}
	
	public static String list2str(List<String> list) {
		StringBuffer listStr = new StringBuffer();
		if(list != null && list.size() > 0) {
			for (String str : list) {
				listStr.append(str + "@");
			}
			listStr.deleteCharAt(listStr.length()-1);
		}
		return listStr.toString();
	}
	
	public static List<String> str2list(String str) {
		List<String> list = new ArrayList<String>();
		if(str != null && !"".equals(str)) {
			String[] strArray = str.split("@");
			for (int i=0; i<strArray.length; i++) {
				list.add(strArray[i]);
			}
		}
		return list;
	}
}
